package com.itheima.day09;

import java.util.HashSet;
import java.util.TreeSet;

//1_ 统计短文中有几句话？挨个打印
//2_ 统计短文中一共有多少个单词(重复的单词算1个),
//    按照字符串长短挨个输出(长度一样,按照字母顺序输出)
//3_ 统计单词the出现了几次?
//4_ 统计字母a出现了几次,在哪些单词中出现过?
public class Test01 {
   private static String str="A feather floats through the air. The falling feather. A city, Savannah, is revealed in the background. The feather floats down toward the city below. The feather drops down toward the street below, as people walk past and cars drive by, and nearly lands on a man's shoulder. He walks across the street, causing the feather to be whisked back on its journey. The feather floats above a stopped car. The car drives off right as the feather floats down toward the street. The feather floats under a passing car, then is sent flying back up in the air. A MAN sits on a bus bench. The feather floats above the ground and finally lands on the man's mud-soaked shoe. The man reached down and picks up the feather. His name is FORREST GUMP. He looks at the feather oddly, moves aside a box of chocolates from an old suitcase, then opens the case. Inside the old suitcase are an assortment of clothes, a ping-pong paddle, toothpaste and other personal items. Forrest pulls out a book titled 'Curious George,' then places the feather inside the book. Forrest closes the suitcase. Something in his eyes reveals that Forrest may not be all there. Forrest looks right as the sound of an arriving bus is heard. A bus pulls up. Forrest remains on the bus bench as the bus continues on. A BLACK WOMAN in a nurse's outfit steps up and sits down at the bus bench next to Forrest. The nurse begins to read a magazine as Forrest looks at her.  Hello. My name's Forrest Gump. He opens a box of chocolates and holds it out for the nurse. Forrest turns up the sidelines and runs toward the end zone. Some opposing players fall down. Forrest runs along the sidelines. The opposing players try to catch him. Forrest runs into the end zone as an opposing player dives at his feet. The referee holds up his arm, singaling a touch down. The crowd cheers wildly. Forrest continues to run, smashing through the band members, then all the way toward the team tunnel. The football coach looks at an asistant coach.Forrest steps into Jenny's bedroom. He carries a tray with breakfast on it. Forrest looks at Jenny as she sleeps. Slowly she wakes up and looks at Forrest. Forrest carries a suitcase as he runs down the road. Forrest runs past the row of mailboxes and turns into the drive. Louise and others are on the front porch.";

    public static void main(String[] args) {
        String[] split = split(str);
        System.out.println("语句的个数是"+split.length);
        System.out.println("每一句分别是:");
        for (String s : split) {
            System.out.println(s);
        }
        //2_ 统计短文中一共有多少个单词(重复的单词算1个),
        TreeSet<String> set = splitWord(str);
        System.out.println("统计短文中一共有"+set.size()+"个单词");
        for (String s : set) {
            System.out.println(s);
        }
        //3_ 统计单词the出现了几次?
        String replace = str.replace(".", " ").replace(",", " ");
        String[] words = replace.split(" ");
        int count=0;
        for (String word : words) {
            if(word.equalsIgnoreCase("the")){
                count++;
            }
        }
        System.out.println("the的个数是"+count);
        //4_ 统计字母a出现了几次,在哪些单词中出现过?
        int countA=0;
        for (char c : str.toCharArray()) {
            if(c=='a'||c=='A'){
                countA++;
            }
        }



        HashSet<String> setA = new HashSet<>();
        for (String word : words) {
            if(word.contains("a") || word.contains("A")){
                setA.add(word);
            }
        }
        System.out.println("a出现了"+countA+"次");
        System.out.println("a在这写单词中出现了");
        for (String s : setA) {
            System.out.println(s);
        }
    }
        //将字符串转换成 语句
    public static String[] split(String str){
        return str.split("\\.");
    }
    public static TreeSet<String> splitWord(String str){
        String replace = str.replace(".", " ").replace(",", " ");
        String[] split = replace.split(" ");
        TreeSet<String> set = new TreeSet<>(
                (s1,s2)->{
                    int i = s2.length() - s1.length();
                    return i == 0 ? s1.compareTo(s2) : i;
                }
        );
        for (String s : split) {
            set.add(s);
        }
        return set;
    }



}
